<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>PatriciaST.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="PatriciaST code in Java">
<meta NAME="TITLE" CONTENT="PatriciaST code in Java">
<meta NAME="KEYWORDS" CONTENT="PatriciaST,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>PatriciaST.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "PatriciaST.java">PatriciaST.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac PatriciaST.java</span>
<span class="comment"> *  Execution:    java PatriciaST</span>
<span class="comment"> *  Dependencies: StdOut.java StdRandom.java Queue.java</span>
<span class="comment"> *  Data files:   n/a</span>
<span class="comment"> *</span>
<span class="comment"> *  A symbol table implementation based on PATRICIA.</span>
<span class="comment"> *</span>
<span class="comment"> *  % java PatriciaST 1000000 1</span>
<span class="comment"> *  Creating dataset (1000000 items)...</span>
<span class="comment"> *  Shuffling...</span>
<span class="comment"> *  Adding (1000000 items)...</span>
<span class="comment"> *  Iterating...</span>
<span class="comment"> *  1000000 items iterated</span>
<span class="comment"> *  Shuffling...</span>
<span class="comment"> *  Deleting (500000 items)...</span>
<span class="comment"> *  Iterating...</span>
<span class="comment"> *  500000 items iterated</span>
<span class="comment"> *  Checking...</span>
<span class="comment"> *  500000 items found and 500000 (deleted) items missing</span>
<span class="comment"> *  Deleting the rest (500000 items)...</span>
<span class="comment"> *  PASS 1 TESTS SUCCEEDED</span>
<span class="comment"> *  %</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;code&gt;</span><span class="comment">PatriciaST</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> class provides an implementation of an unordered</span>
<span class="comment"> *  symbol table of key-value pairs, with the restriction that the key is of</span>
<span class="comment"> *  class {</span><span class="type">@link</span><span class="comment"> java.lang.String}. It supports the usual </span><span class="keyword">&lt;em&gt;</span><span class="comment">put</span><span class="keyword">&lt;/em&gt;</span><span class="comment">,</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">get</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">contains</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">delete</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">size</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, and</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">is-empty</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> methods. It also provides a </span><span class="keyword">&lt;em&gt;</span><span class="comment">keys</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> method for</span>
<span class="comment"> *  iterating over all of the keys. A symbol table implements the</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">associative array</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> abstraction: when associating a value with a key</span>
<span class="comment"> *  that is already in the symbol table, the convention is to replace the old</span>
<span class="comment"> *  value with the new value. Unlike {</span><span class="type">@link</span><span class="comment"> java.util.Map}, this class uses the</span>
<span class="comment"> *  convention that values cannot be </span><span class="keyword">&lt;code&gt;</span><span class="comment">null</span><span class="keyword">&lt;/code&gt;</span><span class="preproc">&amp;mdash;</span><span class="comment">setting the value</span>
<span class="comment"> *  associated with a key to </span><span class="keyword">&lt;code&gt;</span><span class="comment">null</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> is equivalent to deleting the key</span>
<span class="comment"> *  from the symbol table.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  This unordered symbol table class implements PATRICIA (Practical Algorithm</span>
<span class="comment"> *  to Retrieve Information Coded In Alphanumeric). In spite of the acronym,</span>
<span class="comment"> *  string keys are not limited to alphanumeric content. A key may possess any</span>
<span class="comment"> *  string value, except for the string of zero length (the empty string).</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  Unlike other generic symbol table implementations that can accept a</span>
<span class="comment"> *  parameterized key type, this symbol table class can only accommodate keys</span>
<span class="comment"> *  of class {</span><span class="type">@link</span><span class="comment"> java.lang.String}. This unfortunate restriction stems from a</span>
<span class="comment"> *  limitation in Java. Although Java provides excellent support for generic</span>
<span class="comment"> *  programming, the current infrastructure somewhat limits generic collection</span>
<span class="comment"> *  implementations to those that employ comparison-based or hash-based methods.</span>
<span class="comment"> *  PATRICIA does not employ comparisons or hashing; instead, it relies on</span>
<span class="comment"> *  bit-test operations. Because Java does not furnish any generic abstractions</span>
<span class="comment"> *  (or implementations) for bit-testing the contents of an object, providing</span>
<span class="comment"> *  support for generic keys using PATRICIA does not seem practical.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  PATRICIA is a variation of a trie, and it is often classified as a</span>
<span class="comment"> *  space-optimized trie. In a classical trie, each level represents a</span>
<span class="comment"> *  subsequent digit in a key. In PATRICIA, nodes only exist to identify the</span>
<span class="comment"> *  digits (bits) that distinguish the individual keys within the trie. Because</span>
<span class="comment"> *  PATRICIA uses a radix of two, each node has only two children, like a binary</span>
<span class="comment"> *  tree. Also like a binary tree, the number of nodes, within the trie, equals</span>
<span class="comment"> *  the number of keys. Consequently, some classify PATRICIA as a tree.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  The analysis of PATRICIA is complicated. The theoretical wost-case</span>
<span class="comment"> *  performance for a </span><span class="keyword">&lt;em&gt;</span><span class="comment">get</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">put</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, or </span><span class="keyword">&lt;em&gt;</span><span class="comment">delete</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> operation</span>
<span class="comment"> *  is </span><span class="keyword">&lt;strong&gt;</span><span class="comment">O(N)</span><span class="keyword">&lt;/strong&gt;</span><span class="comment">, when </span><span class="keyword">&lt;strong&gt;</span><span class="comment">N</span><span class="keyword">&lt;/strong&gt;</span><span class="comment"> is less than</span>
<span class="comment"> *  </span><span class="keyword">&lt;strong&gt;</span><span class="comment">W</span><span class="keyword">&lt;/strong&gt;</span><span class="comment"> (where </span><span class="keyword">&lt;strong&gt;</span><span class="comment">W</span><span class="keyword">&lt;/strong&gt;</span><span class="comment"> is the length in bits of the</span>
<span class="comment"> *  longest key), and </span><span class="keyword">&lt;strong&gt;</span><span class="comment">O(W)</span><span class="keyword">&lt;/strong&gt;</span><span class="comment">, when </span><span class="keyword">&lt;strong&gt;</span><span class="comment">N</span><span class="keyword">&lt;/strong&gt;</span><span class="comment"> is greater</span>
<span class="comment"> *  than </span><span class="keyword">&lt;strong&gt;</span><span class="comment">W</span><span class="keyword">&lt;/strong&gt;</span><span class="comment">. However, the worst case is unlikely to occur with</span>
<span class="comment"> *  typical use. The average (and usual) performance of PATRICIA is</span>
<span class="comment"> *  approximately </span><span class="keyword">&lt;strong&gt;</span><span class="comment">~lg N</span><span class="keyword">&lt;/strong&gt;</span><span class="comment"> for each </span><span class="keyword">&lt;em&gt;</span><span class="comment">get</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">put</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, or</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">delete</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> operation. Although this appears to put PATRICIA on the same</span>
<span class="comment"> *  footing as binary trees, this time complexity represents the number of</span>
<span class="comment"> *  single-bit test operations (under PATRICIA), and not full-key comparisons</span>
<span class="comment"> *  (as required by binary trees). After the single-bit tests conclude, PATRICIA</span>
<span class="comment"> *  requires just one full-key comparison to confirm the existence (or absence)</span>
<span class="comment"> *  of the key (per </span><span class="keyword">&lt;em&gt;</span><span class="comment">get</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">put</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, or </span><span class="keyword">&lt;em&gt;</span><span class="comment">delete</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> operation).</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  In practice, decent implementations of PATRICIA can often outperform</span>
<span class="comment"> *  balanced binary trees, and even hash tables. Although this particular</span>
<span class="comment"> *  implementation performs well, the source code was written with an emphasis</span>
<span class="comment"> *  on clarity, and not performance. PATRICIA performs admirably when its</span>
<span class="comment"> *  bit-testing loops are well tuned. Consider using the source code as a guide,</span>
<span class="comment"> *  should you need to produce an optimized implementation, for anther key type,</span>
<span class="comment"> *  or in another programming language.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  Other resources for PATRICIA:</span><span class="keyword">&lt;br&gt;</span>
<span class="comment"> *  Sedgewick, R. (1990) </span><span class="keyword">&lt;i&gt;</span><span class="comment">Algorithms in C</span><span class="keyword">&lt;/i&gt;</span><span class="comment">, Addison-Wesley</span><span class="keyword">&lt;br&gt;</span>
<span class="comment"> *  Knuth, D. (1973) </span><span class="keyword">&lt;i&gt;</span><span class="comment">The Art of Computer Programming</span><span class="keyword">&lt;/i&gt;</span><span class="comment">, Addison-Wesley</span><span class="keyword">&lt;br&gt;</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> John Hentosh (based on an implementation by Robert Sedgewick)</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">PatriciaST</span><span class="symbol">&lt;</span><span class="normal">Value</span><span class="symbol">&gt;</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> head</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="normal"> count</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/* An inner Node class specifies the objects that hold each key-value pair.</span>
<span class="comment">     * The b value indicates the relevant bit position.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">Node</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> left</span><span class="symbol">,</span><span class="normal"> right</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> key</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Value</span><span class="normal"> val</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="normal"> b</span><span class="symbol">;</span>

<span class="normal">        </span><span class="keyword">public</span><span class="normal"> </span><span class="function">Node</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> key</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Value</span><span class="normal"> val</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> b</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">key </span><span class="symbol">=</span><span class="normal"> key</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">val </span><span class="symbol">=</span><span class="normal"> val</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">b </span><span class="symbol">=</span><span class="normal"> b</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Initializes an empty PATRICIA-based symbol table.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="comment">/* The constructor creates a head (sentinel) node that contains a</span>
<span class="comment">     * zero-length string.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">PatriciaST</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        head </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Node</span><span class="symbol">(</span><span class="string">""</span><span class="symbol">,</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">);</span>
<span class="normal">        head</span><span class="symbol">.</span><span class="normal">left </span><span class="symbol">=</span><span class="normal"> head</span><span class="symbol">;</span>
<span class="normal">        head</span><span class="symbol">.</span><span class="normal">right </span><span class="symbol">=</span><span class="normal"> head</span><span class="symbol">;</span>
<span class="normal">        count </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Places a key-value pair into the symbol table. If the table already</span>
<span class="comment">     * contains the specified key, then its associated value becomes updated.</span>
<span class="comment">     * If the value provided is </span><span class="keyword">&lt;code&gt;</span><span class="comment">null</span><span class="keyword">&lt;/code&gt;</span><span class="comment">, then the key becomes removed</span>
<span class="comment">     * from the symbol table.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> key the key</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> val the value</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if </span><span class="keyword">&lt;code&gt;</span><span class="comment">key</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> is </span><span class="keyword">&lt;code&gt;</span><span class="comment">null</span><span class="keyword">&lt;/code&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if </span><span class="keyword">&lt;code&gt;</span><span class="comment">key</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> is the empty string.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">put</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> key</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Value</span><span class="normal"> val</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">key </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"called put(null)"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"invalid key"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">val </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="function">delete</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">Node</span><span class="normal"> p</span><span class="symbol">;</span>
<span class="normal">        </span><span class="usertype">Node</span><span class="normal"> x </span><span class="symbol">=</span><span class="normal"> head</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">do</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            p </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">safeBitTest</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">b</span><span class="symbol">))</span><span class="normal"> x </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal">                       x </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span><span class="normal"> </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">p</span><span class="symbol">.</span><span class="normal">b </span><span class="symbol">&lt;</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">b</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">key</span><span class="symbol">.</span><span class="function">equals</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> b </span><span class="symbol">=</span><span class="normal"> </span><span class="function">firstDifferingBit</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> key</span><span class="symbol">);</span>
<span class="normal">            x </span><span class="symbol">=</span><span class="normal"> head</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">do</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                p </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">;</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">safeBitTest</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">b</span><span class="symbol">))</span><span class="normal"> x </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">;</span>
<span class="normal">                </span><span class="keyword">else</span><span class="normal">                       x </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span><span class="normal"> </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">p</span><span class="symbol">.</span><span class="normal">b </span><span class="symbol">&lt;</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">b </span><span class="symbol">&amp;&amp;</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">b </span><span class="symbol">&lt;</span><span class="normal"> b</span><span class="symbol">);</span>
<span class="normal">            </span><span class="usertype">Node</span><span class="normal"> t </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Node</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> val</span><span class="symbol">,</span><span class="normal"> b</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">safeBitTest</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> b</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                t</span><span class="symbol">.</span><span class="normal">left  </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">;</span>
<span class="normal">                t</span><span class="symbol">.</span><span class="normal">right </span><span class="symbol">=</span><span class="normal"> t</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                t</span><span class="symbol">.</span><span class="normal">left  </span><span class="symbol">=</span><span class="normal"> t</span><span class="symbol">;</span>
<span class="normal">                t</span><span class="symbol">.</span><span class="normal">right </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">safeBitTest</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> p</span><span class="symbol">.</span><span class="normal">b</span><span class="symbol">))</span><span class="normal"> p</span><span class="symbol">.</span><span class="normal">right </span><span class="symbol">=</span><span class="normal"> t</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal">                       p</span><span class="symbol">.</span><span class="normal">left  </span><span class="symbol">=</span><span class="normal"> t</span><span class="symbol">;</span>
<span class="normal">            count</span><span class="symbol">++;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">val </span><span class="symbol">=</span><span class="normal"> val</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Retrieves the value associated with the given key.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> key the key</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the value associated with the given key if the key is in the</span>
<span class="comment">     * symbol table and </span><span class="keyword">&lt;code&gt;</span><span class="comment">null</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> if the key is not in the symbol table</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if </span><span class="keyword">&lt;code&gt;</span><span class="comment">key</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> is </span><span class="keyword">&lt;code&gt;</span><span class="comment">null</span><span class="keyword">&lt;/code&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if </span><span class="keyword">&lt;code&gt;</span><span class="comment">key</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> is the empty string.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Value</span><span class="normal"> </span><span class="function">get</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> key</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">key </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"called get(null)"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"invalid key"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">Node</span><span class="normal"> p</span><span class="symbol">;</span>
<span class="normal">        </span><span class="usertype">Node</span><span class="normal"> x </span><span class="symbol">=</span><span class="normal"> head</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">do</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            p </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">safeBitTest</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">b</span><span class="symbol">))</span><span class="normal"> x </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal">                       x </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span><span class="normal"> </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">p</span><span class="symbol">.</span><span class="normal">b </span><span class="symbol">&lt;</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">b</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">key</span><span class="symbol">.</span><span class="function">equals</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">val</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal">                   </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Removes a key and its associated value from the symbol table, if it</span>
<span class="comment">     * exists.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> key the key</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if </span><span class="keyword">&lt;code&gt;</span><span class="comment">key</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> is </span><span class="keyword">&lt;code&gt;</span><span class="comment">null</span><span class="keyword">&lt;/code&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if </span><span class="keyword">&lt;code&gt;</span><span class="comment">key</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> is the empty string.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">delete</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> key</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">key </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"called delete(null)"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"invalid key"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">Node</span><span class="normal"> g</span><span class="symbol">;</span><span class="normal">             </span><span class="comment">// previous previous (grandparent)</span>
<span class="normal">        </span><span class="usertype">Node</span><span class="normal"> p </span><span class="symbol">=</span><span class="normal"> head</span><span class="symbol">;</span><span class="normal">      </span><span class="comment">// previous (parent)</span>
<span class="normal">        </span><span class="usertype">Node</span><span class="normal"> x </span><span class="symbol">=</span><span class="normal"> head</span><span class="symbol">;</span><span class="normal">      </span><span class="comment">// node to delete</span>
<span class="normal">        </span><span class="keyword">do</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            g </span><span class="symbol">=</span><span class="normal"> p</span><span class="symbol">;</span>
<span class="normal">            p </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">safeBitTest</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">b</span><span class="symbol">))</span><span class="normal"> x </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal">                       x </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span><span class="normal"> </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">p</span><span class="symbol">.</span><span class="normal">b </span><span class="symbol">&lt;</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">b</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">key</span><span class="symbol">.</span><span class="function">equals</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="usertype">Node</span><span class="normal"> z</span><span class="symbol">;</span>
<span class="normal">            </span><span class="usertype">Node</span><span class="normal"> y </span><span class="symbol">=</span><span class="normal"> head</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">do</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal">            </span><span class="comment">// find the true parent (z) of x</span>
<span class="normal">                z </span><span class="symbol">=</span><span class="normal"> y</span><span class="symbol">;</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">safeBitTest</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">.</span><span class="normal">b</span><span class="symbol">))</span><span class="normal"> y </span><span class="symbol">=</span><span class="normal"> y</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">;</span>
<span class="normal">                </span><span class="keyword">else</span><span class="normal">                       y </span><span class="symbol">=</span><span class="normal"> y</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span><span class="normal"> </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">y </span><span class="symbol">!=</span><span class="normal"> x</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">==</span><span class="normal"> p</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal">   </span><span class="comment">// case 1: remove (leaf node) x</span>
<span class="normal">                </span><span class="usertype">Node</span><span class="normal"> c</span><span class="symbol">;</span><span class="normal">     </span><span class="comment">// child of x</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">safeBitTest</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">b</span><span class="symbol">))</span><span class="normal"> c </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">;</span>
<span class="normal">                </span><span class="keyword">else</span><span class="normal">                       c </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">;</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">safeBitTest</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> z</span><span class="symbol">.</span><span class="normal">b</span><span class="symbol">))</span><span class="normal"> z</span><span class="symbol">.</span><span class="normal">right </span><span class="symbol">=</span><span class="normal"> c</span><span class="symbol">;</span>
<span class="normal">                </span><span class="keyword">else</span><span class="normal">                       z</span><span class="symbol">.</span><span class="normal">left  </span><span class="symbol">=</span><span class="normal"> c</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal">          </span><span class="comment">// case 2: p replaces (internal node) x</span>
<span class="normal">                </span><span class="usertype">Node</span><span class="normal"> c</span><span class="symbol">;</span><span class="normal">     </span><span class="comment">// child of p</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">safeBitTest</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> p</span><span class="symbol">.</span><span class="normal">b</span><span class="symbol">))</span><span class="normal"> c </span><span class="symbol">=</span><span class="normal"> p</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">;</span>
<span class="normal">                </span><span class="keyword">else</span><span class="normal">                       c </span><span class="symbol">=</span><span class="normal"> p</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">;</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">safeBitTest</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> g</span><span class="symbol">.</span><span class="normal">b</span><span class="symbol">))</span><span class="normal"> g</span><span class="symbol">.</span><span class="normal">right </span><span class="symbol">=</span><span class="normal"> c</span><span class="symbol">;</span>
<span class="normal">                </span><span class="keyword">else</span><span class="normal">                       g</span><span class="symbol">.</span><span class="normal">left  </span><span class="symbol">=</span><span class="normal"> c</span><span class="symbol">;</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">safeBitTest</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> z</span><span class="symbol">.</span><span class="normal">b</span><span class="symbol">))</span><span class="normal"> z</span><span class="symbol">.</span><span class="normal">right </span><span class="symbol">=</span><span class="normal"> p</span><span class="symbol">;</span>
<span class="normal">                </span><span class="keyword">else</span><span class="normal">                       z</span><span class="symbol">.</span><span class="normal">left  </span><span class="symbol">=</span><span class="normal"> p</span><span class="symbol">;</span>
<span class="normal">                p</span><span class="symbol">.</span><span class="normal">left </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">;</span>
<span class="normal">                p</span><span class="symbol">.</span><span class="normal">right </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">;</span>
<span class="normal">                p</span><span class="symbol">.</span><span class="normal">b </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">b</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            count</span><span class="symbol">--;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns </span><span class="keyword">&lt;code&gt;</span><span class="comment">true</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> if the key-value pair, specified by the given</span>
<span class="comment">     * key, exists within the symbol table.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> key the key</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;code&gt;</span><span class="comment">true</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> if this symbol table contains the given</span>
<span class="comment">     * </span><span class="keyword">&lt;code&gt;</span><span class="comment">key</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> and </span><span class="keyword">&lt;code&gt;</span><span class="comment">false</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if </span><span class="keyword">&lt;code&gt;</span><span class="comment">key</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> is </span><span class="keyword">&lt;code&gt;</span><span class="comment">null</span><span class="keyword">&lt;/code&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if </span><span class="keyword">&lt;code&gt;</span><span class="comment">key</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> is the empty string.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">contains</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> key</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">get</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns </span><span class="keyword">&lt;code&gt;</span><span class="comment">true</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> if the symbol table is empty.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;code&gt;</span><span class="comment">true</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> if this symbol table is empty and</span>
<span class="comment">     * </span><span class="keyword">&lt;code&gt;</span><span class="comment">false</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="type">boolean</span><span class="normal"> </span><span class="function">isEmpty</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> count </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the number of key-value pairs within the symbol table.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the number of key-value pairs within this symbol table</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="type">int</span><span class="normal"> </span><span class="function">size</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> count</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns all keys in the symbol table as an </span><span class="keyword">&lt;code&gt;</span><span class="comment">Iterable</span><span class="keyword">&lt;/code&gt;</span><span class="comment">.</span>
<span class="comment">     * To iterate over all of the keys in the symbol table named</span>
<span class="comment">     * </span><span class="keyword">&lt;code&gt;</span><span class="comment">st</span><span class="keyword">&lt;/code&gt;</span><span class="comment">, use the foreach notation:</span>
<span class="comment">     * </span><span class="keyword">&lt;code&gt;</span><span class="comment">for (Key key : st.keys())</span><span class="keyword">&lt;/code&gt;</span><span class="comment">.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> all keys in the sybol table as an </span><span class="keyword">&lt;code&gt;</span><span class="comment">Iterable</span><span class="keyword">&lt;/code&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Iterable&lt;String&gt;</span><span class="normal"> </span><span class="function">keys</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">Queue&lt;String&gt;</span><span class="normal"> queue </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Queue</span><span class="symbol">&lt;</span><span class="normal">String</span><span class="symbol">&gt;();</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">head</span><span class="symbol">.</span><span class="normal">left  </span><span class="symbol">!=</span><span class="normal"> head</span><span class="symbol">)</span><span class="normal"> </span><span class="function">keys</span><span class="symbol">(</span><span class="normal">head</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">,</span><span class="normal">  </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> queue</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">head</span><span class="symbol">.</span><span class="normal">right </span><span class="symbol">!=</span><span class="normal"> head</span><span class="symbol">)</span><span class="normal"> </span><span class="function">keys</span><span class="symbol">(</span><span class="normal">head</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> queue</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> queue</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">keys</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> b</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Queue&lt;String&gt;</span><span class="normal"> queue</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">b </span><span class="symbol">&gt;</span><span class="normal"> b</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="function">keys</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">,</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">b</span><span class="symbol">,</span><span class="normal"> queue</span><span class="symbol">);</span>
<span class="normal">            queue</span><span class="symbol">.</span><span class="function">enqueue</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">key</span><span class="symbol">);</span>
<span class="normal">            </span><span class="function">keys</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">,</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">b</span><span class="symbol">,</span><span class="normal"> queue</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/* The safeBitTest function logically appends a terminating sequence (when</span>
<span class="comment">     * required) to extend (logically) the string beyond its length.</span>
<span class="comment">     *</span>
<span class="comment">     * The inner loops of the get and put methods flow much better when they</span>
<span class="comment">     * are not concerned with the lengths of strings, so a trick is employed to</span>
<span class="comment">     * allow the get and put methods to view every string as an "infinite"</span>
<span class="comment">     * sequence of bits. Logically, every string gets a '\uffff' character,</span>
<span class="comment">     * followed by an "infinite" sequence of '\u0000' characters, appended to</span>
<span class="comment">     * the end.</span>
<span class="comment">     *</span>
<span class="comment">     * Note that the '\uffff' character serves to mark the end of the string,</span>
<span class="comment">     * and it is necessary. Simply padding with '\u0000' is insufficient to</span>
<span class="comment">     * make all unique Unicode strings "look" unique to the get and put methods</span>
<span class="comment">     * (because these methods do not regard string lengths).</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">safeBitTest</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> key</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> b</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">b </span><span class="symbol">&lt;</span><span class="normal"> key</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> </span><span class="number">16</span><span class="symbol">)</span><span class="normal">      </span><span class="keyword">return</span><span class="normal"> </span><span class="function">bitTest</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> b</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">b </span><span class="symbol">&gt;</span><span class="normal"> key</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> </span><span class="number">16</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="number">15</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span><span class="normal">   </span><span class="comment">// padding</span>
<span class="normal">        </span><span class="comment">/* 16 bits of 0xffff */</span><span class="normal">         </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span><span class="normal">    </span><span class="comment">// end marker</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">bitTest</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> key</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> b</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">b </span><span class="symbol">&gt;&gt;&gt;</span><span class="normal"> </span><span class="number">4</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&gt;&gt;&gt;</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">b </span><span class="symbol">&amp;</span><span class="normal"> </span><span class="number">0xf</span><span class="symbol">))</span><span class="normal"> </span><span class="symbol">&amp;</span><span class="normal"> </span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/* Like the safeBitTest function, the safeCharAt function makes every</span>
<span class="comment">     * string look like an "infinite" sequence of characters. Logically, every</span>
<span class="comment">     * string gets a '\uffff' character, followed by an "infinite" sequence of</span>
<span class="comment">     * '\u0000' characters, appended to the end.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">safeCharAt</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> key</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> i</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">i </span><span class="symbol">&lt;</span><span class="normal"> key</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">())</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> key</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">i </span><span class="symbol">&gt;</span><span class="normal"> key</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">())</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="number">0x0000</span><span class="symbol">;</span><span class="normal">            </span><span class="comment">// padding</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal">                  </span><span class="keyword">return</span><span class="normal"> </span><span class="number">0xffff</span><span class="symbol">;</span><span class="normal">            </span><span class="comment">// end marker</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/* For efficiency's sake, the firstDifferingBit function compares entire</span>
<span class="comment">     * characters first, and then considers the individual bits (once it finds</span>
<span class="comment">     * two characters that do not match). Also, the least significant bits of</span>
<span class="comment">     * an individual character are examined first. There are many Unicode</span>
<span class="comment">     * alphabets where most (if not all) of the "action" occurs in the least</span>
<span class="comment">     * significant bits.</span>
<span class="comment">     *</span>
<span class="comment">     * Notice that the very first character comparison excludes the</span>
<span class="comment">     * least-significant bit. The firstDifferingBit function must never return</span>
<span class="comment">     * zero; otherwise, a node would become created as a child to the head</span>
<span class="comment">     * (sentinel) node that matches the bit-index value (zero) stored in the</span>
<span class="comment">     * head node. This would violate the invariant that bit-index values</span>
<span class="comment">     * increase as you descend into the trie.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">firstDifferingBit</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> k1</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> k2</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> c1 </span><span class="symbol">=</span><span class="normal"> </span><span class="function">safeCharAt</span><span class="symbol">(</span><span class="normal">k1</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&amp;</span><span class="normal"> </span><span class="symbol">~</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> c2 </span><span class="symbol">=</span><span class="normal"> </span><span class="function">safeCharAt</span><span class="symbol">(</span><span class="normal">k2</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&amp;</span><span class="normal"> </span><span class="symbol">~</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">c1 </span><span class="symbol">==</span><span class="normal"> c2</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="function">safeCharAt</span><span class="symbol">(</span><span class="normal">k1</span><span class="symbol">,</span><span class="normal"> i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="function">safeCharAt</span><span class="symbol">(</span><span class="normal">k2</span><span class="symbol">,</span><span class="normal"> i</span><span class="symbol">))</span><span class="normal"> i</span><span class="symbol">++;</span>
<span class="normal">            c1 </span><span class="symbol">=</span><span class="normal"> </span><span class="function">safeCharAt</span><span class="symbol">(</span><span class="normal">k1</span><span class="symbol">,</span><span class="normal"> i</span><span class="symbol">);</span>
<span class="normal">            c2 </span><span class="symbol">=</span><span class="normal"> </span><span class="function">safeCharAt</span><span class="symbol">(</span><span class="normal">k2</span><span class="symbol">,</span><span class="normal"> i</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> b </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(((</span><span class="normal">c1 </span><span class="symbol">&gt;&gt;&gt;</span><span class="normal"> b</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&amp;</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="symbol">((</span><span class="normal">c2 </span><span class="symbol">&gt;&gt;&gt;</span><span class="normal"> b</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&amp;</span><span class="normal"> </span><span class="number">1</span><span class="symbol">))</span><span class="normal"> b</span><span class="symbol">++;</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> i </span><span class="symbol">*</span><span class="normal"> </span><span class="number">16</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> b</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Unit tests the </span><span class="keyword">&lt;code&gt;</span><span class="comment">PatriciaST</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> data type.</span>
<span class="comment">     * This test fixture runs a series of tests on a randomly generated dataset.</span>
<span class="comment">     * You may specify up to two integer parameters on the command line. The</span>
<span class="comment">     * first parameter indicates the size of the dataset. The second parameter</span>
<span class="comment">     * controls the number of passes (a new random dataset becomes generated at</span>
<span class="comment">     * the start of each pass).</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">PatriciaST&lt;Integer&gt;</span><span class="normal"> st </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> PatriciaST</span><span class="symbol">&lt;</span><span class="normal">Integer</span><span class="symbol">&gt;();</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> limitItem </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1000000</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> limitPass </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> countPass </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">boolean</span><span class="normal"> ok </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>

<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">.</span><span class="normal">length </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> limitItem </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="function">parseInt</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">.</span><span class="normal">length </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> limitPass </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="function">parseInt</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">1</span><span class="symbol">]);</span>

<span class="normal">        </span><span class="keyword">do</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            String</span><span class="symbol">[]</span><span class="normal"> a </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> String</span><span class="symbol">[</span><span class="normal">limitItem</span><span class="symbol">];</span>
<span class="normal">            </span><span class="type">int</span><span class="symbol">[]</span><span class="normal">    v </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[</span><span class="normal">limitItem</span><span class="symbol">];</span>

<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"Creating dataset (%d items)...</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">,</span><span class="normal"> limitItem</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> limitItem</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                a</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="function">toString</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">,</span><span class="normal"> </span><span class="number">16</span><span class="symbol">);</span>
<span class="normal">                v</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> i</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>

<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"Shuffling...</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">);</span>
<span class="normal">            StdRandom</span><span class="symbol">.</span><span class="function">shuffle</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">);</span>

<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"Adding (%d items)...</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">,</span><span class="normal"> limitItem</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> limitItem</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">                st</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="normal">a</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]],</span><span class="normal"> v</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]);</span>

<span class="normal">            </span><span class="type">int</span><span class="normal"> countKeys </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"Iterating...</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> key </span><span class="symbol">:</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">keys</span><span class="symbol">())</span><span class="normal"> countKeys</span><span class="symbol">++;</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"%d items iterated</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">,</span><span class="normal"> countKeys</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">countKeys </span><span class="symbol">!=</span><span class="normal"> limitItem</span><span class="symbol">)</span><span class="normal"> ok </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">countKeys </span><span class="symbol">!=</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">size</span><span class="symbol">())</span><span class="normal"> ok </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>

<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"Shuffling...</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">);</span>
<span class="normal">            StdRandom</span><span class="symbol">.</span><span class="function">shuffle</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">);</span>

<span class="normal">            </span><span class="type">int</span><span class="normal"> limitDelete </span><span class="symbol">=</span><span class="normal"> limitItem </span><span class="symbol">/</span><span class="normal"> </span><span class="number">2</span><span class="symbol">;</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"Deleting (%d items)...</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">,</span><span class="normal"> limitDelete</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> limitDelete</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">                st</span><span class="symbol">.</span><span class="function">delete</span><span class="symbol">(</span><span class="normal">a</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]]);</span>

<span class="normal">            countKeys </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"Iterating...</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> key </span><span class="symbol">:</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">keys</span><span class="symbol">())</span><span class="normal"> countKeys</span><span class="symbol">++;</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"%d items iterated</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">,</span><span class="normal"> countKeys</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">countKeys </span><span class="symbol">!=</span><span class="normal"> limitItem </span><span class="symbol">-</span><span class="normal"> limitDelete</span><span class="symbol">)</span><span class="normal"> ok </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">countKeys </span><span class="symbol">!=</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">size</span><span class="symbol">())</span><span class="normal">               ok </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>

<span class="normal">            </span><span class="type">int</span><span class="normal"> countDelete </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> countRemain </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"Checking...</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> limitItem</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">i </span><span class="symbol">&lt;</span><span class="normal"> limitDelete</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">st</span><span class="symbol">.</span><span class="function">contains</span><span class="symbol">(</span><span class="normal">a</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]]))</span><span class="normal"> countDelete</span><span class="symbol">++;</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">                </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    </span><span class="type">int</span><span class="normal"> val </span><span class="symbol">=</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">get</span><span class="symbol">(</span><span class="normal">a</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]]);</span>
<span class="normal">                    </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">val </span><span class="symbol">==</span><span class="normal"> v</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">])</span><span class="normal"> countRemain</span><span class="symbol">++;</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"%d items found and %d (deleted) items missing</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">,</span>
<span class="normal">                countRemain</span><span class="symbol">,</span><span class="normal"> countDelete</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">countRemain </span><span class="symbol">+</span><span class="normal"> countDelete </span><span class="symbol">!=</span><span class="normal"> limitItem</span><span class="symbol">)</span><span class="normal"> ok </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">countRemain               </span><span class="symbol">!=</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">size</span><span class="symbol">())</span><span class="normal"> ok </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">st</span><span class="symbol">.</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal">                           ok </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>

<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"Deleting the rest (%d items)...</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">,</span>
<span class="normal">                limitItem </span><span class="symbol">-</span><span class="normal"> countDelete</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> countDelete</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> limitItem</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">                st</span><span class="symbol">.</span><span class="function">delete</span><span class="symbol">(</span><span class="normal">a</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]]);</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">st</span><span class="symbol">.</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> ok </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>

<span class="normal">            countPass</span><span class="symbol">++;</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ok</span><span class="symbol">)</span><span class="normal"> StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"PASS %d TESTS SUCCEEDED</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">,</span><span class="normal"> countPass</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal">    StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"PASS %d TESTS FAILED</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">,</span><span class="normal">    countPass</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span><span class="normal"> </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ok </span><span class="symbol">&amp;&amp;</span><span class="normal"> countPass </span><span class="symbol">&lt;</span><span class="normal"> limitPass</span><span class="symbol">);</span>

<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">ok</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">lang</span><span class="symbol">.</span><span class="function">RuntimeException</span><span class="symbol">(</span><span class="string">"TESTS FAILED"</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>
<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
